home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 19 advanced win32 techniques / win32techniquesdemo / eventlogform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  9.4 KB  |  243 lines

  1. Public Class EventLogForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents btnListLogs As System.Windows.Forms.Button
  26.     Friend WithEvents txtOut As System.Windows.Forms.TextBox
  27.     Friend WithEvents btnListApplication As System.Windows.Forms.Button
  28.     Friend WithEvents btnSQLEntries As System.Windows.Forms.Button
  29.     Friend WithEvents btnSQLEvents As System.Windows.Forms.Button
  30.     Friend WithEvents btnWrite As System.Windows.Forms.Button
  31.  
  32.     'Required by the Windows Form Designer
  33.     Private components As System.ComponentModel.Container
  34.  
  35.     'NOTE: The following procedure is required by the Windows Form Designer
  36.     'It can be modified using the Windows Form Designer.  
  37.     'Do not modify it using the code editor.
  38.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  39.         Me.txtOut = New System.Windows.Forms.TextBox()
  40.         Me.btnListLogs = New System.Windows.Forms.Button()
  41.         Me.btnSQLEvents = New System.Windows.Forms.Button()
  42.         Me.btnListApplication = New System.Windows.Forms.Button()
  43.         Me.btnSQLEntries = New System.Windows.Forms.Button()
  44.         Me.btnWrite = New System.Windows.Forms.Button()
  45.         Me.SuspendLayout()
  46.         '
  47.         'txtOut
  48.         '
  49.         Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  50.                     Or System.Windows.Forms.AnchorStyles.Left) _
  51.                     Or System.Windows.Forms.AnchorStyles.Right)
  52.         Me.txtOut.Location = New System.Drawing.Point(160, 16)
  53.         Me.txtOut.Multiline = True
  54.         Me.txtOut.Name = "txtOut"
  55.         Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
  56.         Me.txtOut.Size = New System.Drawing.Size(344, 288)
  57.         Me.txtOut.TabIndex = 1
  58.         Me.txtOut.Text = ""
  59.         Me.txtOut.WordWrap = False
  60.         '
  61.         'btnListLogs
  62.         '
  63.         Me.btnListLogs.Location = New System.Drawing.Point(16, 16)
  64.         Me.btnListLogs.Name = "btnListLogs"
  65.         Me.btnListLogs.Size = New System.Drawing.Size(120, 40)
  66.         Me.btnListLogs.TabIndex = 0
  67.         Me.btnListLogs.Text = "List Event logs"
  68.         '
  69.         'btnSQLEvents
  70.         '
  71.         Me.btnSQLEvents.Location = New System.Drawing.Point(16, 160)
  72.         Me.btnSQLEvents.Name = "btnSQLEvents"
  73.         Me.btnSQLEvents.Size = New System.Drawing.Size(120, 40)
  74.         Me.btnSQLEvents.TabIndex = 0
  75.         Me.btnSQLEvents.Text = "Get MSSQL events"
  76.         '
  77.         'btnListApplication
  78.         '
  79.         Me.btnListApplication.Location = New System.Drawing.Point(16, 64)
  80.         Me.btnListApplication.Name = "btnListApplication"
  81.         Me.btnListApplication.Size = New System.Drawing.Size(120, 40)
  82.         Me.btnListApplication.TabIndex = 0
  83.         Me.btnListApplication.Text = "List Application entries"
  84.         '
  85.         'btnSQLEntries
  86.         '
  87.         Me.btnSQLEntries.Location = New System.Drawing.Point(16, 112)
  88.         Me.btnSQLEntries.Name = "btnSQLEntries"
  89.         Me.btnSQLEntries.Size = New System.Drawing.Size(120, 40)
  90.         Me.btnSQLEntries.TabIndex = 0
  91.         Me.btnSQLEntries.Text = "List SQL Server entries"
  92.         '
  93.         'btnWrite
  94.         '
  95.         Me.btnWrite.Location = New System.Drawing.Point(16, 208)
  96.         Me.btnWrite.Name = "btnWrite"
  97.         Me.btnWrite.Size = New System.Drawing.Size(120, 40)
  98.         Me.btnWrite.TabIndex = 0
  99.         Me.btnWrite.Text = "Write event logs"
  100.         '
  101.         'EventLogForm
  102.         '
  103.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  104.         Me.ClientSize = New System.Drawing.Size(520, 317)
  105.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWrite, Me.btnSQLEvents, Me.btnSQLEntries, Me.btnListApplication, Me.txtOut, Me.btnListLogs})
  106.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  107.         Me.Name = "EventLogForm"
  108.         Me.Text = "Event Logging"
  109.         Me.ResumeLayout(False)
  110.  
  111.     End Sub
  112.  
  113. #End Region
  114.  
  115.     ' a routine that shows output in the textbox control
  116.  
  117.     Sub LogMessage(ByVal msg As String)
  118.         txtOut.AppendText(msg & ControlChars.CrLf)
  119.     End Sub
  120.  
  121.     ' List existing logs.
  122.  
  123.     Private Sub btnListLogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListLogs.Click
  124.         txtOut.Text = ""
  125.         Dim evlog As EventLog
  126.         For Each evlog In EventLog.GetEventLogs
  127.             LogMessage(evlog.Log)
  128.         Next
  129.     End Sub
  130.  
  131.     ' list the Application event log
  132.  
  133.     Private Sub btnListApplication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListApplication.Click
  134.         txtOut.Text = ""
  135.  
  136.         ' Get a reference to the Application eventlog.
  137.         Dim elApp As New EventLog("Application")
  138.         Dim entry As EventLogEntry
  139.  
  140.         ' NOTE: this loop usually generates too much info to be displayed in the textbox
  141.         '       so expect that the output be truncated at some point.
  142.         For Each entry In elApp.Entries
  143.             LogMessage(GetEntryInfo(entry))
  144.         Next
  145.     End Sub
  146.  
  147.     ' Return readable information on an event log entry.
  148.  
  149.     Private Function GetEntryInfo(ByVal entry As EventLogEntry) As String
  150.         ' This schema is similar to the info you see in Event Viewer.
  151.         Dim sb As New System.Text.StringBuilder(200)
  152.         sb.Append(entry.EntryType.ToString)
  153.         sb.Append("  ")
  154.         sb.Append(entry.TimeGenerated)
  155.         sb.Append("  ")
  156.         sb.Append(entry.Source)
  157.         sb.Append("  ")
  158.         sb.Append(entry.Category)
  159.         sb.Append("  ")
  160.         sb.Append(entry.EventID)
  161.         sb.Append("  ")
  162.         sb.Append(entry.UserName)
  163.         sb.Append("  ")
  164.         sb.Append(entry.Message)
  165.         sb.Append("  ")
  166.  
  167.         Return sb.ToString
  168.  
  169.     End Function
  170.  
  171.     ' display log messages from SQL Server
  172.  
  173.     Private Sub btnSQLEntries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQLEntries.Click
  174.         txtOut.Text = ""
  175.  
  176.         ' Get a reference to the Application eventlog.
  177.         Dim elApp As New EventLog("Application", ".")
  178.         Dim entry As EventLogEntry
  179.  
  180.         For Each entry In elApp.Entries
  181.             If entry.Source = "MSSQLServer" Then
  182.                 LogMessage(GetEntryInfo(entry))
  183.             End If
  184.         Next
  185.     End Sub
  186.  
  187.     ' enable trapping events from SQL Server
  188.  
  189.     Private Sub btnSQLEvents_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQLEvents.Click
  190.         ' We're interested in SQL Server events on local machine only.
  191.         Dim sourceName As String = "MSSQLServer"
  192.  
  193.         ' Exit if the source isn't registered.
  194.         If Not EventLog.SourceExists(sourceName) Then Exit Sub
  195.         ' Get the name of the log that contains entries from SQL Server.
  196.         Dim logName As String = EventLog.LogNameFromSourceName(sourceName, ".")
  197.         ' Get the corresponding EventLog object.
  198.         Dim evLog As New EventLog(logName)
  199.         ' Associate it to an event handler.
  200.         AddHandler evLog.EntryWritten, AddressOf EntryWritten
  201.         ' This is very important.
  202.         evLog.EnableRaisingEvents = True
  203.  
  204.         txtOut.Text = ""
  205.     End Sub
  206.  
  207.     ' this event fires when an entry is written to the log by SQL Server
  208.  
  209.     Private Sub EntryWritten(ByVal sender As Object, ByVal e As System.Diagnostics.EntryWrittenEventArgs)
  210.         If e.Entry.Source = "MSSQLServer" Then
  211.             LogMessage(GetEntryInfo(e.Entry))
  212.         End If
  213.     End Sub
  214.  
  215.     ' write to the event log
  216.  
  217.     Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
  218.         ' Register your demo app as an event source.
  219.         EventLog.CreateEventSource("DemoApp2", "XXX")
  220.         ' Create an EventLog object connected to that event log.
  221.         Dim evLog As New EventLog("XXX", ".", "DemoApp2")
  222.  
  223.         ' Write two entries to the Application log.
  224.         evLog.WriteEntry("First message")
  225.         System.Threading.Thread.Sleep(500)
  226.         evLog.WriteEntry("Second message")
  227.  
  228.         ' Write an error message.
  229.         evLog.WriteEntry("Third message", EventLogEntryType.Error)
  230.         ' Write a warning error with an application-defined event id.
  231.         evLog.WriteEntry("Fourth message", EventLogEntryType.Warning, 123)
  232.         ' Write a warning error with an application-defined event id and category id.
  233.         evLog.WriteEntry("Fifth message", EventLogEntryType.Warning, 123, 456)
  234.         ' Write a warning error with an application-defined event id and category id,
  235.         ' plus associated binary data.
  236.         Dim bytes() As Byte = {0, 2, 4, 6, 8, 10, 12}
  237.         evLog.WriteEntry("Fifth message", EventLogEntryType.Warning, 123, 456, bytes)
  238.  
  239.         ' Delete this event source.
  240.         EventLog.DeleteEventSource("DemoApp2")
  241.     End Sub
  242. End Class
  243.